package net.lovesrc.allin.biz.dida;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalTime;

@Data
public class Ride {

    private Long id;

    private Double sOriginLat;

    private Double sOriginLng;

    private Double sLat;

    private Double sLng;

    private String sName;

    private String sAddress;

    private Double eOriginLat;

    private Double eOriginLng;

    private Double eLat;

    private Double eLng;

    private String eName;

    private String eAddress;

    private String sTime1;

    private String sTime2;

    private Integer sDistance;

    private Integer eDistance;

    private String name;

    private Long nextRunTime;

    private boolean enabled;

    public LocalTime getTargetSTime1() {
        if (StringUtils.isBlank(sTime1)) {
            return LocalTime.now();
        }
        if ("now".equalsIgnoreCase(sTime1)) {
            return LocalTime.now();
        }
        return LocalTime.parse(sTime1);
    }

    public LocalTime getTargetSTime2() {
        LocalTime sTime1 = getTargetSTime1();
        if (StringUtils.isBlank(sTime2)) {
            return sTime1.plusMinutes(15);
        }
        if (sTime2.startsWith("+")) {
            LocalTime nowTime = LocalTime.now();
            LocalTime maxTime = null;
            String diffTimeString = sTime2;
            if (sTime2.contains(",")) {
                maxTime = LocalTime.parse(sTime2.substring(sTime2.indexOf(",") + 1));
                diffTimeString = sTime2.substring(0, sTime2.indexOf(","));
            }
            int diffTime = Integer.valueOf(diffTimeString.substring(1));
            LocalTime resultTime;
            if (nowTime.isBefore(sTime1)) {
                resultTime = sTime1.plusMinutes(diffTime);
            } else {
                resultTime = nowTime.plusMinutes(diffTime);
            }
            if (maxTime != null && resultTime.isAfter(maxTime)) {
                resultTime = maxTime;
            }
            return resultTime;
        }
        return LocalTime.parse(sTime2);
    }

}
